/*
 * GKO3ContestElement.java
 * Created on 23 juin 08
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2008, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.contest;


import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.dao.match.GKOMatch;
import com.hockeo.vhbase.data.dao.season.GMatchday;
import com.hockeo.vhbase.data.dao.season.GMoveday;
import com.hockeo.vhbase.data.dao.user.GComputerUser;
import com.hockeo.vhbase.error.GNotImplementedException;

/**
 * Represents a best-of-three KO contest element.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@DiscriminatorValue( "KO3" )
public class GKO3ContestElement extends GContestElement
{
  @Column( name = "ko_num_matches" )
  private int            d_nNumMatches;

  @OneToMany( mappedBy = "d_contestElement", cascade = { CascadeType.REMOVE, CascadeType.REFRESH } )
  @OrderBy( "d_nId" )
  private List<GKOMatch> d_listMatches;

  /** Default parameterless constructor. */
  protected GKO3ContestElement()
  {
    super();
  }

  /**
   * Creates a new best-of-three KO contest element.
   *
   * @param parent the parent round
   * @param strId the unique contest element ID
   * @param strLabel the label or resource key
   * @param nNumMatches the number of match pairings for this element
   */
  public GKO3ContestElement( GContestRound parent, String strId, String strLabel, int nNumMatches )
  {
    super( parent, strId, strLabel );

    d_nNumMatches = nNumMatches;
  }

  /**
   * Returns all attached KO matches.
   */
  public List<GKOMatch> getKOMatches()
  {
    return d_listMatches;
  }

  @Override
  public int getNumTeams()
  {
    return d_nNumMatches * 2;
  }

  /*
   * @see com.hockeo.vhbase.data.GContestElement#attachMatchdaysToMovedays(java.util.List,
   * java.util.List)
   */
  @Override
  public void attachMatchdaysToMovedays( List<GMoveday> listMovedays, List<GMatchday> listMatchdays, boolean fUseMiddleMoveday )
  {
    throw new GNotImplementedException( "This method should not be called for KO round elements." );
  }

  /**
   * A KO round is always played at a single moveday.
   *
   * @see com.hockeo.vhbase.data.dao.contest.GContestElement#getMinNumMovedays()
   */
  @Override
  public int getMinNumMovedays()
  {
    return 1;
  }

  /**
   * In a best of three KO series, three matchdays are always foreseen (though the third
   * one may not be required).
   *
   * @see com.hockeo.vhbase.data.dao.contest.GContestElement#getNumMatchdays()
   */
  @Override
  public int getNumMatchdays()
  {
    return 3;
  }

  @Override
  public String getMoveMakingImplName( GComputerUser user )
  {
    return user.getImplNameKO3();
  }

  /**
   * Returns the number of KO matches to be played in this contest element.
   */
  public int getNumMatches()
  {
    return d_nNumMatches;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GKO3ContestElement.class;
  }
}
